package atessitura;
import java.util.ArrayList;


public class Chords 
{
	private int [] tuning;
	private static int [] standard = {64,59,55,50,45,40};
	private static int [] dropD = {64,59,55,50,45,38};
	private static int [] dropC = {62,57,53,48,43,36};
	private static int [] halfStepDown = {63,58,54,49,44,39};
	
	public Chords (int [] tuning)
	{
		this.tuning=tuning; // high string to low string
	}
	
	public ArrayList <GuitarNote> convertNotes (ArrayList<GuitarNote> input)
	{
		
		convert (input);		
		return input;
	}
	
	public void convert (ArrayList <GuitarNote> notes)
	{
		if (equalArrays(standard, tuning)) // standard tuning
		{
			
		}
		if (equalArrays(dropD, tuning))
		{
			convertDropD(notes);
			
		}
	}
	
	private boolean equalArrays (int [] arr1, int [] arr2)
	{
		
		for (int x= 0; x < arr1.length;x++)
		{
			if (arr1[x]!=arr2[x])
			{
				return false;
			}
			
		}
		return true;
	}
	
	private void convertDropD (ArrayList<GuitarNote> list)
	{
		sortNotes (list); // lowest notes first
		int num1 = getNoteNumber(list.get(0)); // the notenumber of lowest note
		int num2 = getNoteNumber(list.get(1)); // notenumber of second lowest note
		int diff = num2-num1;
		if (diff == 7) // we got a power chord
		{
			convertDropDPowerChord(list, num1, num2);
			
		}
		/*if (diff == 12)
		{
			GuitarNote n1 = list.get(0);
			GuitarNote n2 = list.get(1);
			if (getNoteNumber(n1) >= 38 && getNoteNumber(n1) <= 50)
			{
				n1.setString(str)
			}
		}*/
		
		
		

	}

	private void convertDropDPowerChord(ArrayList<GuitarNote> list, int num1, int num2) 
	{
		int diff;
		if (num1 <=45) // if this probably should be on the 6th string
		{
			GuitarNote part1 = list.get(0);
			int stringNum1 = 5;
			int fret1 = num1- dropD[5];
			part1.setString(stringNum1);
			part1.setFret(fret1);
			GuitarNote part2 = list.get(1); // this is the second guitar note of the chord, or second lowest notenumber
			int stringNum = 4; // since the difference is 7, it has to be on the 5th string (aka array loc 4)
			int fret = num2 - dropD[4]; // this will get us the fret
			part2.setString(stringNum); // putting it on the right string
			part2.setFret(fret); // putting it on the right fret
			//part2.setString();
			//part2.setFret();
		}
		else if (num1 <= 52)
		{
			GuitarNote part1 = list.get(0);
			GuitarNote part2 = list.get(1);
			int stringNum1 = 4;
			int fret1 = num1 - dropD[4];
			part1.setString(stringNum1);
			part1.setFret(fret1);
			int stringNum2 = 3;
			int fret2 = num2-dropD[3];
			part2.setString(stringNum2);
			part2.setFret(fret2);
			
		}
		
		if (list.size() == 3) // we got a bigger chord
		{
			if (num2 <=52)
			{
				int num3 = getNoteNumber(list.get(2));
				diff = num3-num2;
				if (diff == 5)
				{
					GuitarNote part3 = list.get(2); // this is the third guitar note of the chord, or third lowest notenumber
					int stringNum = 3; // since the difference is 5, it has to be on the 4th string (aka array loc 3)
					int fret = num3 - dropD[3]; // this will get us the fret
					part3.setString(stringNum); // putting it on the right string
					part3.setFret(fret); // putting it on the right fret
				}
			}
			else if (num2 <=57)
			{
				int num3 = getNoteNumber(list.get(2));
				diff = num3-num2;
				if (diff == 5)
				{
					GuitarNote part3 = list.get(2); // this is the third guitar note of the chord, or third lowest notenumber
					int stringNum = 2; // since the difference is 5, it has to be on the 4th string (aka array loc 3)
					int fret = num3 - dropD[2]; // this will get us the fret
					part3.setString(stringNum); // putting it on the right string
					part3.setFret(fret); // putting it on the right fret
				}
				
			}
		}
	}
	
	public int getNoteNumber (GuitarNote note)
	{
		int fret;
		int string;
		int noteNum = 0;;
		if (equalArrays(tuning, dropD))
		{
			fret = note.getFret();
			string = note.getString();
			noteNum = dropD[string] + fret; 
			return noteNum;
		}
		else if (equalArrays(tuning, standard))
		{
			
			fret = note.getFret();
			string = note.getString();
			noteNum = standard[string] + fret; 
		}
		
		return noteNum;
	}
	
	private void sortNotes (ArrayList<GuitarNote> list) // low to high
	{
		for (int x = 0; x < list.size()-1; x++)
		{
			int num = getNoteNumber(list.get(x));
			int swap = x;
			for (int y = x+1; y<list.size(); y++)
			{
				if (getNoteNumber(list.get(y)) < num)
				{
					num = getNoteNumber(list.get(y));
					swap =y;
				}
			}
			swap (list, x,swap);
		}
	}
	
	private void swap (ArrayList<GuitarNote> list, int x, int y)
	{
		GuitarNote temp = list.get(x);
		list.set(x, list.get(y));
		list.set(y, temp);
		
	}
}
